package com.song.ejf.action;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.song.ejf.page.Page;
import com.song.ejf.pojo.SMenu;
import com.song.ejf.service.MenuService;
import com.song.ejf.util.UUIDUtil;

public class MenuAction extends ActionSupport {

	private static final long serialVersionUID = -6609393044471160489L;
	Log log = LogFactory.getLog(MenuAction.class);
	public SMenu menu;
	public String checkItems;
	public String resName;
	public Page page;
	@Autowired()
	public MenuService menuService;
	/**
	 * 准备添加菜单
	 * @return
	 */
	public String pAddMenu(){
		return "pAddMenu";
	}
	/**
	 * 添加资源.
	 * @return
	 */
	public String addMenu(){
		try {
			if(menu != null){
				menu.setId(UUIDUtil.makeUUID());
				menuService.saveOrUpdateMenu(menu);
				addActionMessage("添加资源成功!");
			}else{
				addActionMessage("添加资源失败!");
			}
		} catch (Exception e) {
			log.error("添加菜单异常.",e);
		}
		return "addMenu";
	}
	/**
	 * 列出所有资源
	 * @return
	 */
	public String listMenu(){
		try {
			if(page == null){
				page = new Page();
			}
			if(menu == null){
				menu = new SMenu();
			}
			menuService.loadMenuPage(page, menu.getMenuName(),resName);
		} catch (Exception e) {
			log.error("菜单列表异常.",e);
		}
		return "listMenu";
	}
	/**
	 * 删除资源
	 * @return
	 */
	public String delMenu(){
		try {
			if(StringUtils.isBlank(checkItems)){
				addActionMessage("删除资源失败!");
				return "delResource";
			}
			boolean flag = menuService.delMenu(checkItems.split(","));
			if(flag){
				addActionMessage("删除资源成功!");
			}else{
				addActionMessage("删除资源失败!");
			}
		} catch (Exception e) {
			log.error("删除菜单异常.",e);
		}
		return "delMenu";
	}
	/**
	 * 准备编辑资源
	 * @return
	 */
	public String pEditMenu(){
		try {
			menu = menuService.findMenuById(menu.getId());
		} catch (Exception e) {
			log.error("跳转到编辑菜单页面异常.",e);
		}
		return "pEditMenu";
	}
	/**
	 * 编辑资源
	 * @return
	 */
	public String editMenu(){
		try {
			if(menu != null){
				menuService.saveOrUpdateMenu(menu);
				addActionMessage("修改资源成功!");
			}else{
				addActionMessage("修改资源失败!");
			}
		} catch (Exception e) {
			log.error("编辑菜单异常.",e);
		}
		return "editMenu";
	}
	
	////////////////////////////////////////////////////////
	public SMenu getMenu() {
		return menu;
	}
	public void setMenu(SMenu menu) {
		this.menu = menu;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public String getCheckItems() {
		return checkItems;
	}
	public void setCheckItems(String checkItems) {
		this.checkItems = checkItems;
	}
	public String getResName() {
		return resName;
	}
	public void setResName(String resName) {
		this.resName = resName;
	}
}
